<template>
  <div class="addEdit-block">
    <el-form
      class="detail-form-content"
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="80px"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item class="input" label="设备编号" prop="shebeibianhao">
            <el-input v-model="ruleForm.shebeibianhao" placeholder="设备编号" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="input" label="设备名称" prop="shebeimingcheng">
            <el-input v-model="ruleForm.shebeimingcheng" placeholder="设备名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="input" label="设备类型" prop="shebeileixing">
            <el-input v-model="ruleForm.shebeileixing" placeholder="设备类型" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="input" label="是否在线" prop="zhuangtai">
            <el-select v-model="ruleForm.zhuangtai" placeholder="请选择是否在线">
              <el-option label="在线" value="在线" />
              <el-option label="离线" value="离线" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="input" label="数量" prop="shuliang">
            <el-input v-model="ruleForm.shuliang" placeholder="数量" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item class="textarea" label="注意事项" prop="zhuyishixiang">
            <el-input type="textarea" :rows="4" v-model="ruleForm.zhuyishixiang" placeholder="注意事项" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item class="btn">
        <el-button type="primary" class="btn-success" @click="onSubmit">提交</el-button>
        <el-button class="btn-close" @click="back">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getShebeiList, addShebei, updateShebei } from '@/utils/shebeixinxiLocal';
export default {
  data() {
    return {
      ruleForm: {
        id: '',
        shebeibianhao: '',
        shebeimingcheng: '',
        shebeileixing: '',
        zhuangtai: '',
        shuliang: '',
        zhuyishixiang: '',
      },
      rules: {
        shebeibianhao: [
          { required: true, message: '设备编号不能为空', trigger: 'blur' },
        ],
        shebeimingcheng: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' },
        ],
        shuliang: [
          { required: true, message: '数量不能为空', trigger: 'blur' },
        ],
      },
    };
  },
  methods: {
    init(id) {
      if (id) {
        const list = getShebeiList();
        const item = list.find(i => i.id === id);
        if (item) {
          this.ruleForm = { ...item };
        }
      } else {
        this.ruleForm = {
          id: '',
          shebeibianhao: Date.now(),
          shebeimingcheng: '',
          shebeileixing: '',
          zhuangtai: '',
          shuliang: '',
          zhuyishixiang: '',
        };
      }
    },
    onSubmit() {
      this.$refs["ruleForm"].validate(valid => {
        if (!valid) return;
        if (this.ruleForm.id) {
          updateShebei(this.ruleForm);
          this.$message.success('修改成功');
        } else {
          addShebei(this.ruleForm);
          this.$message.success('新增成功');
        }
        this.$emit('update:visible', false);
        this.$parent.addOrUpdateFlag = false;
        this.$parent.getDataList();
      });
    },
    back() {
      this.$parent.addOrUpdateFlag = false;
    },
  },
};
</script>
